package com.tianyu.service.tcp.send;

import com.tianyu.common.ByteModel;

import java.nio.ByteBuffer;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2024/8/1 - 17:10
 **********************************/

public class ManualFlight implements TcpClientChannelSend {
    public void sendTakeOff(Integer companyId, String boxSn, float flyingAltitude) {
        byte[] content = new byte[6];
        content[0] = 0x00;
        content[1] = 0x11;
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putFloat(flyingAltitude);
        System.arraycopy(buffer.array(), 0, content, 2, 4);
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public void sendReturnHome(Integer companyId, String boxSn) {
        byte[] content = new byte[2];
        content[1] = 0x12;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public void sendCancelReturnHome(Integer companyId, String boxSn) {
        byte[] content = new byte[2];
        content[1] = 0x13;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }


    public void sendLand(Integer companyId, String boxSn) {
        byte[] content = new byte[2];
        content[1] = 0x14;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public void sendCancelLand(Integer companyId, String boxSn) {
        byte[] content = new byte[2];
        content[1] = 0x15;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public void sendForceLand(Integer companyId, String boxSn) {
        byte[] content = new byte[2];
        content[1] = 0x29;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public void sendFlyControl(Integer companyId, String boxSn,
                               Float fbSpeed,
                               Float lrSpeed,
                               Float udSpeed,
                               Float yaw,
                               Short executionTime) {
        byte[] content = new byte[20];
        content[1] = 0x28;
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putFloat(fbSpeed);
        System.arraycopy(buffer.array(), 0, content, 2, 4);
        buffer.clear();
        buffer.putFloat(lrSpeed);
        System.arraycopy(buffer.array(), 0, content, 6, 4);
        buffer.clear();
        buffer.putFloat(udSpeed);
        System.arraycopy(buffer.array(), 0, content, 10, 4);
        buffer.clear();
        buffer.putFloat(yaw);
        System.arraycopy(buffer.array(), 0, content, 14, 4);
        buffer.clear();
        buffer.putShort(executionTime);
        System.arraycopy(buffer.array(), 0, content, 18, 2);
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    /**
     * @param companyId
     * @param boxSn
     * @param flag      0x00-解除紧急制动 0x01-紧急制动
     */
    public void sendEmergencyStop(Integer companyId, String boxSn, byte flag) {
        byte[] content = new byte[3];
        content[1] = 0x32;
        content[2] = flag;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    /**
     * @param companyId
     * @param boxSn
     * @param flag      0x00 云盒获取控制权, 0x01云盒释放控制权
     */
    public void sendControlAuthority(Integer companyId, String boxSn, byte flag) {
        byte[] content = new byte[3];
        content[1] = 0x30;
        content[2] = flag;
        this.sendContent(companyId, boxSn, (byte) 0xD1, content);
    }

    public static void main(String[] args) {
        new ManualFlight().sendFlyControl(17323, "M26220230415043", 0.1F, 0F, 0.1F, 0F, (short) 2);
    }
}
